package be.kuleuven.thesis.neo4j;

import java.io.IOException;

import org.neo4j.graphdb.DynamicLabel;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.UniqueFactory;

import be.kuleuven.thesis.neo4j.Neo4jDBManager.RelTypes;
import be.kuleuven.thesis.parsers.VCFreader;
import be.kuleuven.thesis.parsers.VariantFeature;
import be.kuleuven.thesis.parsers.VariantList;


public class ImportingSNPs {
	private static Neo4jDBManager dbManager;
	private static GraphDatabaseService myGraphDb;
	private static Label snpLabel = DynamicLabel.label("snp");
	private static String snpPropertyIndex = "subm_id";

	
	public static void main(String[] args) throws IOException {
		VCFreader vcf = new VCFreader();
		VariantList variants = vcf.read("/home/moni/Documents/BioinformaticsMaster/Thesis/DataITM/SNP/SNP/ACC5.Close.191.3.snp1.vcf.gz");
		String batchName = vcf.getBatchName();
		String referenceFile = vcf.getReference();
		
		dbManager = new Neo4jDBManager();
		myGraphDb = dbManager.getGraphDb();
		dbManager.createIndexLabelProperty(snpLabel, snpPropertyIndex);
		
		Label batchLabel = DynamicLabel.label("batchAnalysis");
		String batchIndex = "batch_name";
		dbManager.createIndexLabelProperty(batchLabel, batchIndex);
		
/*		Label sampleLabel = DynamicLabel.label("DNAsample");
		String sampleIndex = "sanger_code";
		dbManager.createIndexLabelProperty(sampleLabel, sampleIndex);*/
		
		try ( Transaction tx = dbManager.getGraphDb().beginTx()) {
			
			Node batchNode = dbManager.getOrCreateNode(batchLabel, batchIndex, batchName);
			batchNode.setProperty("reference", referenceFile);
			batchNode.setProperty("Sample_size", vcf.sampleSize());
			
			int i = 0;
			for(VariantFeature variant : variants){
				String submission_id = "ss".concat(Integer.toString(i));
				i++;
				Node snpNode = CreateSNP(variant, submission_id);
				snpNode.createRelationshipTo(batchNode, RelTypes.SUBMISSION);
				
				
			}
			tx.success();
		}

	}
	
	public static Node CreateSNP(VariantFeature variant, String sub_id){
		Node node;
	
		try ( Transaction tx = myGraphDb.beginTx()) {
			ResourceIterator<Node> snpNodes = dbManager.graphDb.findNodesByLabelAndProperty(snpLabel, 
					snpPropertyIndex, sub_id ).iterator();
			if(snpNodes.hasNext()){
				System.out.print("The SNP node already exists, returning already created node");
				node = snpNodes.next();
				snpNodes.close();
			}else{
				node = myGraphDb.createNode(snpLabel);
				node.setProperty(snpPropertyIndex, sub_id);
				node.setProperty("chrom", variant.chrom());
				node.setProperty("position", variant.position());
				node.setProperty("ref_allele",variant.refAllele());
				node.setProperty("alt_allele",variant.altAllele());
				node.setProperty("local_id",variant.localId());
				node.setProperty("info",variant.info());
				node.setProperty("quality",variant.quality());
			}
			tx.success();
		}
		
		return node;
	}
		

}
